package com.echo.register.common.handler;

import com.echo.register.common.exception.RegisterException;
import com.echo.register.common.result.Result;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @ClassName: GlobalExceptionHandler
 * @Package: com.echo.register.common.handler
 * @Description: 全局异常处理类
 * @Author 谢同学
 * @Create 2023/12/10 19:48
 * @Version 1.0
 */
@ControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result error(Exception e){
        e.printStackTrace();
        return Result.fail();
    }

    /**
     * 自定义异常处理方法
     * @param e
     * @return
     */
    @ExceptionHandler(RegisterException.class)
    @ResponseBody
    public Result error(RegisterException e){
        e.printStackTrace();
        return Result.build(e.getCode(), e.getMessage());
    }

}
